//Copyright 2011 University of Southampton 
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Administration;

namespace UniversityOfSouthampton.Taxonomy.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("7ea38a2d-b262-4eab-8282-d769e24f5176")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        //public override void FeatureActivated(SPFeatureReceiverProperties properties)
        //{
        //}

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            
            using (SPSite site = new SPSite(SPContext.Current.Web.Url ))//TODO: fix
            {
               
                using (SPWeb web = site.OpenWeb())
                {
                    SPUserCodeService userCodeService = SPUserCodeService.Local;
                    SPProxyOperationType proxyOperationType;

                    //Create Tax group
                    proxyOperationType = new SPProxyOperationType(TaxonomyCreateGroup.ProxyAssemblyName, TaxonomyCreateGroup.ProxOperationTypeName);                   
                    userCodeService.ProxyOperationTypes.Add(proxyOperationType);
                    userCodeService.Update(true);

                    //Create Set group
                    proxyOperationType = new SPProxyOperationType(TaxonomyCreateTermSet.ProxyAssemblyName, TaxonomyCreateTermSet.ProxOperationTypeName);
                    userCodeService.ProxyOperationTypes.Add(proxyOperationType);
                    userCodeService.Update(true);

                    //Create field
                    proxyOperationType = new SPProxyOperationType(TaxonomyCreateField.ProxyAssemblyName, TaxonomyCreateField.ProxOperationTypeName);
                    userCodeService.ProxyOperationTypes.Add(proxyOperationType);
                    userCodeService.Update(true);


                     
                }
            }
        }



        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPUserCodeService userCodeService = SPUserCodeService.Local;
            SPProxyOperationType proxyOperationType;

            //Create group
            proxyOperationType = new SPProxyOperationType(TaxonomyCreateGroup.ProxyAssemblyName, TaxonomyCreateGroup.ProxOperationTypeName);
            userCodeService.ProxyOperationTypes.Remove(proxyOperationType);
            userCodeService.Update(true);

            //Create Set group
            proxyOperationType = new SPProxyOperationType(TaxonomyCreateTermSet.ProxyAssemblyName, TaxonomyCreateTermSet.ProxOperationTypeName);
            userCodeService.ProxyOperationTypes.Remove(proxyOperationType);
            userCodeService.Update(true);


            //Create field
            proxyOperationType = new SPProxyOperationType(TaxonomyCreateField.ProxyAssemblyName, TaxonomyCreateField.ProxOperationTypeName);
            userCodeService.ProxyOperationTypes.Remove(proxyOperationType);
            userCodeService.Update(true);

           
        }


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
